package cn.com.controller;

import cn.com.beans.ActivationCode;
import cn.com.beans.VipActivateReturn;
import cn.com.service.ActivationCodeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/insertActivation")
public class InsertActivationCodeController {
    @Resource
    private ActivationCodeService activationCodeService;

    @GetMapping
    public String home(){
        return "Insert/InsertActivationCode";
    }

    @GetMapping(headers = "X-Requested-With=XMLHttpRequest")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "10") int limit){
        PageHelper.startPage(page,limit);
        List<ActivationCode> all = activationCodeService.findAll();
//        System.out.println(all);
        PageInfo<ActivationCode> pageInfo = new PageInfo<>(all);
        Map<String, Object> result = new HashMap<>();
        result.put("data",pageInfo.getList());
        result.put("code",0);
        result.put("count",pageInfo.getTotal());
        result.put("msg","查询成功");
        return result;
    }
//    public Map<String,Object> findAll(PAccount account, @RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "10") int limit){
//        List<PAccount> pAccounts = service.find(account);
//        PageInfo<PAccount> pageInfo = new PageInfo<>(pAccounts);
//        Map<String,Object> map = new HashMap<>();
//        map.put("data",pageInfo.getList());
//        map.put("code",0);
//        map.put("count",pageInfo.getTotal());
//        map.put("msg","查询成功");
//        return map;
//    }

    @GetMapping("/verify/{codeValue}")
    @ResponseBody
    public List<VipActivateReturn> insert(@PathVariable String codeValue){
        System.out.println(codeValue);
        ActivationCode activationCode = new ActivationCode();
        activationCode.setAcCode(codeValue);
        List<ActivationCode> activationCodes = activationCodeService.selectByACtivationCode(activationCode);
        VipActivateReturn vipActivateReturn = new VipActivateReturn();
        if(activationCodes.size() == 0){
            vipActivateReturn.setMsg("激活码可添加");
            vipActivateReturn.setType(1);
        }else{
            vipActivateReturn.setMsg("不可添加激活码");
            vipActivateReturn.setType(0);
        }
        List<VipActivateReturn> list = new ArrayList<>();
        list.add(vipActivateReturn);
        return list;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Map<String,Object> insert(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        ActivationCode activationCode = new ActivationCode();
        String code = request.getParameter("coName");
        activationCode.setAcCode(code);
        activationCode.setAcType(0);
        activationCodeService.insert(activationCode);
        result.put("code",1);
        result.put("msg","添加成功");
        return result;
    }
}
